if(NOT OS_FREEBSD AND NOT APPLE AND NOT ARCH_ARM)
    option(ENABLE_S3 "Enable S3" ${ENABLE_LIBRARIES})
elseif(ENABLE_S3 OR USE_INTERNAL_AWS_S3_LIBRARY)
    message (${RECONFIGURE_MESSAGE_LEVEL} "Can't use S3 on ARM, Apple or FreeBSD")
endif()

if(NOT ENABLE_S3)
    if(USE_INTERNAL_AWS_S3_LIBRARY)
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't use internal S3 library with ENABLE_S3=OFF")
    endif()
    return()
endif()

option(USE_INTERNAL_AWS_S3_LIBRARY "Set to FALSE to use system S3 instead of bundled (experimental set to OFF on your own risk)"
       ON)

if (NOT USE_INTERNAL_POCO_LIBRARY AND USE_INTERNAL_AWS_S3_LIBRARY)
    message (FATAL_ERROR "Currently S3 support can be built only with internal POCO library")
endif()

if (NOT USE_INTERNAL_AWS_S3_LIBRARY)
    message (${RECONFIGURE_MESSAGE_LEVEL} "Compilation with external S3 library is not supported yet")
endif()

if (NOT EXISTS "${ClickHouse_SOURCE_DIR}/contrib/aws/aws-cpp-sdk-s3")
    message (WARNING "submodule contrib/aws is missing. to fix try run: \n git submodule update --init --recursive")
    if (USE_INTERNAL_AWS_S3_LIBRARY)
        message (${RECONFIGURE_MESSAGE_LEVEL} "Can't find internal S3 library")
    endif ()
    set (MISSING_AWS_S3 1)
endif ()

if (USE_INTERNAL_AWS_S3_LIBRARY AND NOT MISSING_AWS_S3)
    set(AWS_S3_INCLUDE_DIR "${ClickHouse_SOURCE_DIR}/contrib/aws/aws-cpp-sdk-s3/include")
    set(AWS_S3_CORE_INCLUDE_DIR "${ClickHouse_SOURCE_DIR}/contrib/aws/aws-cpp-sdk-core/include")
    set(AWS_S3_LIBRARY aws_s3)
    set(USE_INTERNAL_AWS_S3_LIBRARY 1)
    set(USE_AWS_S3 1)
else()
    message (${RECONFIGURE_MESSAGE_LEVEL} "Can't enable S3")
    set(USE_INTERNAL_AWS_S3_LIBRARY 0)
    set(USE_AWS_S3 0)
endif ()

message (STATUS "Using aws_s3=${USE_AWS_S3}: ${AWS_S3_INCLUDE_DIR} : ${AWS_S3_LIBRARY}")
